﻿using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.PageActionManager;
using ZBJF.Warranty.WebProvider;
using ZBJF.Warranty.WebProvider.Filter;

namespace ZBJF.Warranty.CustomerService.Controllers
{
    public class LoginController : BaseController
    {
        [LoginFilter(IsCheck = false)]
        [AuthenticationFilter(NeedAuthentication = false)]
        public ActionResult Index()
        {
           return View();
        }

        [LoginFilter(IsCheck = false)]
        [AuthenticationFilter(NeedAuthentication = false)]
        [AllowAnonymous]
        public ActionResult NoAuthory()
        {
            return View();
        }

        [HttpPost]
        [LoginFilter(IsCheck = false)]
        [AuthenticationFilter(NeedAuthentication = false)]
        [ValidateAntiForgeryToken]
        public ActionResult Index(SystemUser model)
        {
            var responseResult = new ResponseResult();
            var flag = false;
            var user = ServiceFactory.SystemUserServiceInstance.Login(model.UserAccount, model.Password,ApplicationType.CustomerServiceSite,  out flag);
            if (flag)
            {
                CurrentUser = user;
                CookiesHelper.Instance.SetLoginSession(user.UserAccount);
                CookiesHelper.Instance.SetLoginUserTypeSession(user.BelongApplicationType);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                CurrentUser = null;
                CookiesHelper.Instance.ClearLoginSession();
                responseResult.Message = "用户名或者密码无效！";
                TempData["ResponseResult"] = responseResult;
                return View();
            }
        }

        [HttpGet]
        [AuthenticationFilter(NeedAuthentication = false)]
        [AllowAnonymous]
        public ActionResult LogOut()
        {
            CurrentUser = null;
            CookiesHelper.Instance.ClearLoginSession();
            return RedirectToAction("Index", "Login");
        }

        public ActionResult ModifyPassword(int id)
        {
            var systemUser = ServiceFactory.SystemUserServiceInstance.Load(id);
            var model = new SystemUserModel();
            if (systemUser != null)
            {
                model.UserAccount = systemUser.UserAccount;
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult ModifyPassword(SystemUserModel model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = OpreateType.Update
            };
            var systemUser = ServiceFactory.SystemUserServiceInstance.Load(model.ID);
            if (systemUser == null)
                responseResult.Message = "无法找到该用户信息";
            else
            {
                if (systemUser.Password != CommonMethod.JiaMi(model.Password))
                {
                    responseResult.Message = "原密码错误";
                }
                else if (model.NewPassword != model.ConfirmPassword)
                {
                    responseResult.Message = "两次输入的密码不一致";
                }
                else
                {
                    systemUser.Password = CommonMethod.JiaMi(model.NewPassword);
                    responseResult.Success = ServiceFactory.SystemUserServiceInstance.UpdateEntities(systemUser);
                }
            }
            TempData["ResponseResult"] = responseResult;
            return RedirectToAction("ModifyPassword", new { id = model.ID });
        } 
    }
}